import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Solution522 {
    public int findLUSlength(String[] strs) {
        TreeMap<String,Integer> map=new TreeMap<>((a,b)->a.length()!=b.length()?b.length()-a.length():a.compareTo(b));
        for (String str : strs) {
            map.put(str,map.getOrDefault(str,0)+1);
        }
        Set<String> stringSet=new HashSet<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue()>=2){
                stringSet.add(entry.getKey());
            }else{
                if (judge(stringSet,entry.getKey())){
                    return entry.getKey().length();
                }else{
                    stringSet.add(entry.getKey());
                }
            }
        }
        return -1;
    }

    private boolean judge(Set<String> stringSet, String key) {
        for (String s : stringSet) {
            int j=0;
            for (int i=0;i<s.length();i++){
                if (s.charAt(i)==key.charAt(j)){
                    j++;
                    if (j==key.length()){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new Solution522().findLUSlength(new String[]{"aba","cdc","eae"}));
    }
}
